//
//  PaperProgressView.m
//  JW_BasePorject
//
//  Created by JW on 2018/6/27.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "PaperProgressView.h"


@interface PaperProgressView ()

@property (nonatomic, strong) UIView *sliderBackView;       // 滑动条背景
@property (nonatomic, strong) UIView *sliderView;           // 滑动条
@property (nonatomic, strong) UILabel *numLabel;            // 当前页码

@property (nonatomic, strong) UILabel *titleLabel;          // 视图题目

@end

@implementation PaperProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initConfigureView];
    }
    return self;
}

- (void)initConfigureView {
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.numLabel];
    [self addSubview:self.sliderBackView];
    [self addSubview:self.sliderView];
    
}

#pragma mark - setter
- (void)setSliderTitle:(NSString *)sliderTitle{
    _sliderTitle = sliderTitle;
    self.titleLabel.text = sliderTitle;    
}

- (void)setCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    if (currentIndex != 0) {
        [self updateProgress];
    }
   
}

- (void)setTotalNum:(NSInteger)totalNum{
    _totalNum = totalNum;
    [self updateProgress];

}


#pragma mark - private
- (void)updateProgress{
    
    NSString *currentIndex = [NSString stringWithFormat:@"%ld",self.currentIndex];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:currentIndex attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:18],NSForegroundColorAttributeName: ZhuTiColor}];
    
    NSString *totalNum = [NSString stringWithFormat:@"/%ld",self.totalNum];
    NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:totalNum attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:15],NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
    
    [attributedStr appendAttributedString: totalStr];
    
    self.numLabel.attributedText = attributedStr;
    
    CGFloat progress = (CGFloat)self.currentIndex/(CGFloat)self.totalNum;
    [UIView animateWithDuration:0.5 animations:^{
        self.sliderView.frame = CGRectMake(0, 37, self.frame.size.width * progress, 3.0);
    }];
    
}

#pragma mark - getter

- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.frame.size.width/2-10, 35)];
        _titleLabel.textColor = [UIColor darkGrayColor];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _titleLabel;
}


- (UILabel *)numLabel{
    if (_numLabel == nil) {
        _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2, 0, self.frame.size.width/2-5, 35)];
        _numLabel.textColor = [UIColor darkGrayColor];
        _numLabel.textAlignment = NSTextAlignmentRight;

    }
    return _numLabel;
}

- (UIView *)sliderBackView{
    if (_sliderBackView == nil) {
        _sliderBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 37, self.frame.size.width, 3.0)];
        _sliderBackView.backgroundColor = [UIColor lightGrayColor];
        ViewRadius(_sliderBackView, 1.5);
    }
    return _sliderBackView;
}

- (UIView *)sliderView{
    if (_sliderView == nil) {
        _sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 37, 0, 3.0)];
        _sliderView.backgroundColor = ZhuTiColor;
        ViewRadius(_sliderView, 1.5);
    }
    return _sliderView;
}


@end
